<?php
/*
 *---------------------------------------------------------------------------
 *
 *                  T E N C E N T   P R O P R I E T A R Y
 *
 *     COPYRIGHT (c)  2008 BY  TENCENT  CORPORATION.  ALL RIGHTS
 *     RESERVED.   NO  PART  OF THIS PROGRAM  OR  PUBLICATION  MAY
 *     BE  REPRODUCED,   TRANSMITTED,   TRANSCRIBED,   STORED  IN  A
 *     RETRIEVAL SYSTEM, OR TRANSLATED INTO ANY LANGUAGE OR COMPUTER
 *     LANGUAGE IN ANY FORM OR BY ANY MEANS, ELECTRONIC, MECHANICAL,
 *     MAGNETIC,  OPTICAL,  CHEMICAL, MANUAL, OR OTHERWISE,  WITHOUT
 *     THE PRIOR WRITTEN PERMISSION OF :
 *
 *                        TENCENT  CORPORATION
 *
 *       Advertising Platform R&D Team, Advertising Platform & Products
 *       Tencent Ltd.
 *---------------------------------------------------------------------------
 */

/**
 * Web Request extends TMRequest for web request
 * @package lib.request
 * @version JYWebRequest.class.php
 */
class JYWebRequest extends JYRequest {
	protected
	$languages              = null,
	$charsets               = null,
	$acceptableContentTypes = null,
	$pathInfoArray          = null,
	$relativeUrlRoot        = null,
	$getParameters          = null,
	$postParameters         = null,
	$formats                = array(),
	$format                 = null;

	public function  __construct($parameters = array(), $attributes = array()) {
		parent::__construct($parameters, $attributes);
		$this->initialize($parameters, $attributes);
	}


	/**
	 * Initializes this WebRequest.
	 * @param array $parameters An associative array of initialization parameters
	 * @param array $attributes An associative array of initialization attributes
	 */
	public function initialize($parameters = array(), $attributes = array()) {
		if (isset($_SERVER['REQUEST_METHOD'])) {
			switch ($_SERVER['REQUEST_METHOD']) {
				case 'GET':
					$this->setMethod(self::GET);
					break;

				case 'POST':
					$this->setMethod(self::POST);
					break;

				case 'PUT':
					$this->setMethod(self::PUT);
					break;

				case 'DELETE':
					$this->setMethod(self::DELETE);
					break;

				case 'HEAD':
					$this->setMethod(self::HEAD);
					break;

				default:
					$this->setMethod(self::GET);
			}
		} else {
			// set the default method
			$this->setMethod(self::GET);
		}

		// load parameters from GET/POST
		$this->loadParameters();
	}

	/**
	 * Loads GET, PATH_INFO and POST data into the parameter list.
	 */
	protected function loadParameters() {
		// GET parameters
		//伪静态，处理GET参数
		if(URL_REWRITE) JYUrl::unRewrite();	
		$this->getParameters = get_magic_quotes_gpc() ? JYUtil::stripslashesDeep($_GET) : $_GET;
		foreach($this->getParameters as $key => $parameter) {
			$this->getParameters[$key] = JYUtil::filterTextDeep($parameter);
		}

		// POST parameters
		$this->postParameters = get_magic_quotes_gpc() ? JYUtil::stripslashesDeep($_POST) : $_POST;
		foreach($this->postParameters as $key => $parameter) {
			$this->postParameters[$key] = JYUtil::filterTextDeep($parameter);
		}
	}
	

	//--------------------------File Operation-------------------------------//

	/**
	 * Retrieves the uniform resource identifier for the current web request.
	 * @return string Unified resource identifier
	 */
//	public function getUri() {
//		$pathArray = $this->getPathInfoArray();
//
//		if ($this->isAbsUri()) {
//			return $pathArray['REQUEST_URI'];
//		}
//
//		return $this->getUriPrefix().$pathArray['REQUEST_URI'];
//	}

	/**
	 * See if the client is using absolute uri
	 * @return boolean true, if is absolute uri otherwise false
	 */
//	public function isAbsUri()
//	{
//		$pathArray = $this->getPathInfoArray();
//
//		return preg_match('/^http/', $pathArray['REQUEST_URI']);
//	}

	/**
	 * Returns Uri prefix, including protocol, hostname and server port.
	 * @return string Uniform resource identifier prefix
	 */
//	public function getUriPrefix()
//	{
//		$pathArray = $this->getPathInfoArray();
//		if ($this->isSecure())
//		{
//			$standardPort = '443';
//			$protocol = 'https';
//		}
//		else
//		{
//			$standardPort = '80';
//			$protocol = 'http';
//		}
//
//		$host = explode(":", $pathArray['HTTP_HOST']);
//		if (count($host) == 1)
//		{
//			$host[] = $pathArray['SERVER_PORT'];
//		}
//
//		if ($host[1] == $standardPort || empty($host[1]))
//		{
//			unset($host[1]);
//		}
//
//		return $protocol.'://'.implode(':', $host);;
//	}

	/**
	 * Retrieves the path info for the current web request.
	 * @return string Path info
	 */
//	public function getPathInfo()
//	{
//		$pathInfo = '';
//
//		$pathArray = $this->getPathInfoArray();
//
//
//		if (!isset($pathArray['PATH_INFO']) || !$pathArray['PATH_INFO'])
//		{
//			if (isset($pathArray['REQUEST_URI']))
//			{
//				$script_name = $this->getScriptName();
//				$uri_prefix = $this->isAbsUri() ? $this->getUriPrefix() : '';
//				$pathInfo = preg_replace('/^'.preg_quote($uri_prefix, '/').'/','',$pathArray['REQUEST_URI']);
//				$pathInfo = preg_replace('/^'.preg_quote($script_name, '/').'/', '', $pathInfo);
//				$prefix_name = preg_replace('#/[^/]+$#', '', $script_name);
//				$pathInfo = preg_replace('/^'.preg_quote($prefix_name, '/').'/', '', $pathInfo);
//				$pathInfo = preg_replace('/'.preg_quote($pathArray['QUERY_STRING'], '/').'$/', '', $pathInfo);
//			}
//		}
//		else
//		{
//			$pathInfo = $pathArray['PATH_INFO'];
//			if ($relative_url_root = $this->getRelativeUrlRoot())
//			{
//				$pathInfo = preg_replace('/^'.str_replace('/', '\\/', $relative_url_root).'\//', '', $pathInfo);
//			}
//		}
//
//		if (!$pathInfo)
//		{
//			$pathInfo = '/';
//		}
//
//		return $pathInfo;
//	}

	/**
	 * Get the request get parameters
	 * @return array     the request get parameters
	 */
	public function getGetParameters() {
		return $this->getParameters;
	}

	/**
	 * Get the request post parameters
	 * @return array     the request post parameters
	 */
	public function getPostParameters() {
		return $this->postParameters;
	}

	/**
	 * Checks if the request method is the given one.
	 * @param    string $method    The method name
	 * @return bool true if the current method is the given one, false otherwise
	 */
	public function isMethod($method) {
		return strtolower($method) == strtolower($this->getMethodName());
	}

	/**
	 * Returns the value of a GET parameter.
	 * @param    string $name         The GET parameter name
	 * @param    string $default    The default value
	 * @return string The GET parameter value
	 */
	public function getGetParameter($name, $default = null) {
		if (isset($this->getParameters[$name])) {
			return $this->getParameters[$name];
		} else {
			return $default;
		}
	}

	/**
	 * Returns the value of a POST parameter.
	 * @param    string $name         The POST parameter name
	 * @param    string $default    The default value
	 * @return string The POST parameter value
	 */
	public function getPostParameter($name, $default = null) {
		if (isset($this->postParameters[$name])) {
			return $this->postParameters[$name];
		} else {
			return $default;
		}
	}
}